//
//  JLCFRegisterService.m
//  QTZ
//
//  Created by ski on 2017/8/16.
//  Copyright © 2017年 ski. All rights reserved.
//

#import "JLCFRegisterService.h"
#import "sHkPtEJLCFMineRequestozAYZs.h"

@implementation JLCFRegisterService

+ (JLCFRegisterService *)shareService {
    static JLCFRegisterService *service;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        service = [[JLCFRegisterService alloc] init];
    });
    return service;
}

- (instancetype)init {
    if (self = [super init]) {
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(loadDetailUserInfo)
                                                     name:MINE_REFRESH
                                                   object:nil];
        
    }
    return self;
}

- (BOOL)hasToken {
    NSString *user_token = [[NSUserDefaults standardUserDefaults] objectForKey:USER_TOKEN];
    if (IsEmptyString(user_token)) {
        return NO;
    } else {
        return YES;
    }
}


- (void)setPhone:(NSString *)phone {
    if (phone) {
        NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
        [userDefault setObject:phone forKey:USER_PHONE];
        [userDefault synchronize];
    }
}

- (NSString *)phone {
    NSString *user_phone = [[NSUserDefaults standardUserDefaults] objectForKey:USER_PHONE];
    return user_phone;
}

- (void)setToken:(NSString *)token {
    if (token) {

        NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
        [userDefault setObject:token forKey:USER_TOKEN];
        [userDefault synchronize];
    }
}

- (NSString *)token {
    NSString *user_token = [[NSUserDefaults standardUserDefaults] objectForKey:USER_TOKEN];
    return user_token;
}

- (void)setPayPassword:(NSString *)payPassword {
    if (payPassword) {
        NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
        [userDefault setObject:payPassword forKey:USER_PAYPASSWORD];
        [userDefault synchronize];
    }
}

- (NSString *)payPassword {
    NSString *user_payPassword = [[NSUserDefaults standardUserDefaults] objectForKey:USER_PAYPASSWORD];
    return user_payPassword;
}

- (void)setShareId:(NSString *)shareId {
    if (shareId) {
        NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
        [userDefault setObject:shareId forKey:USER_SHAREID];
        [userDefault synchronize];
    }
}

- (NSString *)shareId {
    NSString *user_shareId = [[NSUserDefaults standardUserDefaults] objectForKey:USER_SHAREID];
    return user_shareId;
}

- (void)logOut {
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    [userDefault removeObjectForKey:USER_PHONE];
    [userDefault removeObjectForKey:USER_TOKEN];
    [userDefault removeObjectForKey:USER_PAYPASSWORD];
    [userDefault removeObjectForKey:USER_SHAREID];
    [userDefault synchronize];
    self.userInfo = nil;
    KPostNotification(KUserDetailInfoChange, nil);
}

- (void)loadDetailUserInfo {
    if (_isLoadingUserInfo) return;
    
    // 获取用户主要信息
    
    if (self.token) {
        [MBProgressHUD showLoadHUDMsg:@"加载中..."];
        _isLoadingUserInfo = YES;
        [sHkPtEJLCFMineRequestozAYZs postMineDataWithParams:nil loadingInView:nil success:^(PBseiKJLCFMineDataModelWqnMcO *mine) {
            [MBProgressHUD hideHUD];
            
            [JLCFRegisterService shareService].userMoney = mine.userMoney;
            [JLCFRegisterService shareService].shareId = mine.shareId;
            
            self.isLoadingUserInfo = NO;
            self.userInfo = mine;
            
            KPostNotification(KUserDetailInfoChange, nil);
            
        } failure:^(NSString *error) {
            [MBProgressHUD hideHUD];
            
            [self logOut];
            self.isLoadingUserInfo = NO;
        }];
    } else {
        KPostNotification(KUserDetailInfoChange, nil);
    }
}


@end
